package com.asiainfo.database;
/*
 * 
 * 数据库操作工具类
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

public class JdbcTemplete {
	//数据库更新方法
	public int update(String sql,Object...args){
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = DB_Utils.getConnection();
			ps = conn.prepareStatement(sql);
			if(args!=null){
				for(int i=0;i<args.length;i++){
					ps.setObject(i+1, args[i]);
				}
			}
			return ps.executeUpdate();
		}catch(SQLException e){
			Logger logger = Logger.getLogger("");
			logger.warning("更新SQL异常");
			e.printStackTrace();
			return -1;
		}finally{
			//关闭数据库连接
			DB_Utils.close(null, ps, conn);
		}
	}
	//查询数据库方法
	public Object quary(String sql,ResultSetHandler handler,Object...args){
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			conn = DB_Utils.getConnection();
			if(conn == null){
				return null;
			}
			ps = conn.prepareStatement(sql);
			if(args!=null){
				for(int i=0;i<args.length;i++){
					ps.setObject(i+1, args[i]);
				}
			}
			rs = ps.executeQuery();
			return handler.doHandler(rs);
		}catch(SQLException e){
			Logger logger = Logger.getLogger("");
			logger.warning("查询SQL异常");
			e.printStackTrace();
			return null;
		}finally{
			//关闭数据库连接
			DB_Utils.close(rs, ps, conn);
		}
	}
}

